local extension = Package("luming_division")
extension.extensionName = "luming"
extension.game_modes_whitelist = {"lm_mode"}
Fk:loadTranslationTable{
  ["luming_division"] = "鹿鸣-割据",
}

local U = require "packages/utility/utility"
local LM = require "packages/luming/LM"

--------------------------------------------------孙权-------------------------------------------------------------
local lm_huju = fk.CreateTriggerSkill{
  name = "lm_huju",
  mute = true,
  events = {fk.GameStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local svts = table.simpleClone(self.lmLordList)
    -- table.shuffle(svts)
    local max = 2
    local num = 2

    svts = table.map(svts, function(s) return LM.initServant(player, s) end)
    LM.setServantsPool(player, svts)
    LM.setServantsMax(player, max)
    local init_svts = table.random(LM.getServantsPool(player), num)
    LM.findServants(player, table.map(init_svts, Util.NameMapper), self.name)
  end
}
local lm_zhiheng = fk.CreateTriggerSkill{
  name = "lm_zhiheng",
  events = {fk.EventPhaseStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    LM.removeServants(player, table.map(LM.getServants(player), Util.NameMapper), player, nil, self.name)
    local choices = {"lm_huju_wenchen", "lm_huju_wujiang"}
    local choice = room:askForChoice(player, choices, self.name, "#lm_zhiheng-ask")
    local found = {}
    local occupied = 0
    while occupied < LM.getServantSlot(player) do
      local find = LM.findServantsByChc(player, 1, 1, 2, self.name, "#lm_zhiheng-find:::" .. choice,
      function(svt)
        return svt.back == choice .. "-lmback"
      end, true)
      if find[1] ~= nil then
        table.insertIfNeed(found, find[1])
        occupied = occupied + LM.getServantSize(player, find[1])
      end
    end
    LM.findServants(player, found, self.name)
    room:setPlayerMark(player, "_lm_zhiheng_prohibit", table.map(found, Util.NameMapper))
    room:setPlayerMark(player, "_lm_zhiheng-turn", 1)
  end,

  refresh_events = {fk.AfterTurnEnd},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("_lm_zhiheng_prohibit") ~= 0 and player:getMark("_lm_zhiheng-turn") == 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "_lm_zhiheng_prohibit", 0)
  end
}
local lm_zhiheng_prohibit = LM.CreateServantSkill{
  name = "#lm_zhiheng_prohibit",
  prohibit_find = function(self, player, svt)
    return table.contains(player:getTableMark("_lm_zhiheng_prohibit"), svt.name)
  end
}
local lm_shicai = fk.CreateTriggerSkill{
  name = "lm_shicai",
  frequency = Skill.Wake,
  events = {LM.servantsRemoved},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  can_wake = function(self, event, target, player, data)
    local logic = player.room.logic
    local svts = {"lm__sunquan_zhouyu", "lm__lvmeng", "lm__lusu"}
    logic:getEventsOfScope(GameEvent[LM.LMRemoveServants], 1, function (e)
      local d = e.data[1] ---@type LMRemoveStruct
      if d.who == player and d.proposer == player then
        for _, svt in ipairs(d.svts) do
          table.removeOne(svts, svt.name)
        end
      end
      return #svts == 0
    end, Player.HistoryGame)
    return #svts == 0
  end,
  on_use = function(self, event, target, player, data)
    LM.findServants(player, {"lm__luxun"}, self.name)
  end
}
local lm__sunquan = General(extension, "lm__sunquan", "lm_k__ge", 5)
lm_zhiheng:addRelatedSkill(lm_zhiheng_prohibit)
lm__sunquan:addSkill(lm_huju)
lm__sunquan:addSkill(lm_zhiheng)
lm__sunquan:addSkill(lm_shicai)
Fk:loadTranslationTable{
  ["lm__sunquan"] = "孙权",
  ["lm_huju"] = "虎踞",
  [":lm_huju"] = "军力技，你的初始将领上限为2。游戏开始时，你随机获得两名将领。",
  ["@[lm_servants]&lm_huju"] = "将池",
  ["@[lm_servants]lm_huju"] = "将领",
  ["lm_zhiheng"] = "制衡",
  [":lm_zhiheng"] = "锁定技，出牌阶段开始时，你移除所有将领，然后从文臣或武将中二选一发现将领直到无将领空位，你下回合不能发现这些将领。",
  ["lm_shicai"] = "识才",
  [":lm_shicai"] = "觉醒技，你移除将领后，若你移除过你的周瑜，吕蒙，鲁肃，你获得陆逊。",
  ["#lm_zhiheng-ask"] = "制衡：你须选择一个类别，然后发现此类别将领直到无空位",
  ["#lm_zhiheng-find"] = "制衡：你发现两个%arg，请选择一个将领获得",

  ["$lm_shicai1"] = "你说得对，但是感觉不如界权，精品第一，史诗质检员",
  ["$lm_shicai2"] = "回合内大制衡找顺拆卡距离随便打，不好打的制衡找无懈闪桃下回合找顺拆继续打，对爆也是界权优",

  ["lm_huju_wenchen"] = "文臣",
  [":lm_huju_wenchen"] = "江东天险，雄兵百万亦难飞越！",
  ["lm_huju_wujiang"] = "武将",
  [":lm_huju_wujiang"] = "顺江而上，建功立业就在此时！",
}

local lm_huju_wenchen = fk.CreateTriggerSkill{
  name = "lm_huju_wenchen"
}
local lm_huju_wujiang = fk.CreateTriggerSkill{
  name = "lm_huju_wujiang"
}

local lm__zhangzhao = General(extension, "lm__zhangzhao", "lm_k__ge", 0)
LM.setlordServant(lm_huju, "lm__zhangzhao")
lm__zhangzhao:addRelatedSkill(lm_huju_wenchen)
lm__zhangzhao.lmSvtBack = "lm_huju_wenchen-lmback"
lm__zhangzhao.hidden = true
local lm_zhuxiang = fk.CreateTriggerSkill{
  name = "lm_zhuxiang",
  anim_type = "drawcard",
  events = {LM.importedServants},
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      return table.find(data.svts, function(svt) return table.contains(LM.getServantSkills(svt), self.name) end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local _, dat = room:askForUseActiveSkill(player, "#lm_zhuxiang_active", "#lm_zhuxiang-discard", true)
    if dat then
      self.cost_data = dat.cards
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(self.cost_data, self.name, player, player)
    room:drawCards(player, 3, self.name)
    room:setPlayerMark(player, "@@lm_zhuxiang-phase", 1)
  end
}
local lm_zhuxiang_prohibit = fk.CreateProhibitSkill{
  name = "#lm_zhuxiang_prohibit",
  prohibit_use = function(self, player, card)
    if card and card.trueName == "slash" then
      return player:getMark("@@lm_zhuxiang-phase") ~= 0
    end
  end
}
local lm_zhuxiang_active = fk.CreateActiveSkill{
  name = "#lm_zhuxiang_active",
  card_num = 2,
  target_num = 0,
  card_filter = function(self, to_select, selected)
    local card = Fk:getCardById(to_select)
    if Self:prohibitDiscard(card) then return end
    if #selected == 0 then
      return card.trueName == "slash"
    else
      return #selected == 1
    end
  end,
}
Fk:addSkill(lm_zhuxiang_active)
lm_zhuxiang:addRelatedSkill(lm_zhuxiang_prohibit)
lm__zhangzhao:addSkill(lm_zhuxiang)
Fk:loadTranslationTable{
  ["lm__zhangzhao"] = "张昭",
  ["lm_zhuxiang"] = "主降",
  [":lm_zhuxiang"] = "当你获得此将领后，你可以弃置两张牌（其中至少一张是【杀】），然后摸三张牌，本阶段你不能使用【杀】。",
  ["#lm_zhuxiang_active"] = "主降",
  ["#lm_zhuxiang-discard"] = "你可以弃置两张牌（其中至少一张是【杀】），然后摸三张牌，本阶段你不能使用【杀】",
  ["@@lm_zhuxiang-phase"] = "主降",
}

local lm__zhanghong = General(extension, "lm__zhanghong", "lm_k__ge", 0)
LM.setlordServant(lm_huju, "lm__zhanghong")
lm__zhanghong:addRelatedSkill("lm_huju_wenchen")
lm__zhanghong.lmSvtBack = "lm_huju_wenchen-lmback"
lm__zhanghong.hidden = true
local lm_fufeng = fk.CreateActiveSkill{
  name = "lm_fufeng",
  anim_type = "offensive",
  max_phase_use_time = 1,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 0,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    player:drawCards(1, self.name, "top", {"@@lm_fufeng-turn-inhand", 1})
    local targets = table.filter(room.alive_players, function(p)
      return p ~= player and player:canPindian(p)
    end)
    if #targets == 0 then return end
    local target = room:getPlayerById(room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#lm_fufeng-pindian", self.name, false)[1])
    local pindian = player:pindian({target}, self.name)
    local loser = {player, target}
    table.removeOne(loser, pindian.results[target.id].winner)
    loser = table.map(loser, Util.IdMapper)
    room:sortPlayersByAction(loser)
    for _, pid in ipairs(loser) do
      room:loseHp(room:getPlayerById(pid), 1, self.name)
    end
  end
}
local lm_fufeng_vs = fk.CreateFilterSkill{
  name = "#lm_fufeng_vs",
  card_filter = function(self, card, player, judge)
    return card:getMark("@@lm_fufeng-turn-inhand") ~= 0
  end,
  view_as = function(self, card)
    return Fk:cloneCard(card.name, card.suit, 7)
  end
}
lm_fufeng:addRelatedSkill(lm_fufeng_vs)
lm__zhanghong:addSkill(lm_fufeng)
Fk:loadTranslationTable{
  ["lm__zhanghong"] = "张纮",
  ["lm_fufeng"] = "赋讽",
  [":lm_fufeng"] = "阶段技，你可以摸一张牌，此牌本回合点数视为7，然后你与一名其他角色拼点，没赢者失去1点体力。",
  ["#lm_fufeng"] = "你可以摸一张牌，此牌本回合点数视为7，然后你与一名其他角色拼点，没赢者失去1点体力",
  ["#lm_fufeng_vs"] = "赋讽",
  ["#lm_fufeng-pindian"] = "赋讽：你须与一名其他角色拼点，没赢者失去1点体力",
  ["@@lm_fufeng-turn-inhand"] = "赋讽",
}

local lm__buzhi = General(extension, "lm__buzhi", "lm_k__ge", 0)
LM.setlordServant(lm_huju, "lm__buzhi")
lm__buzhi:addRelatedSkill("lm_huju_wenchen")
lm__buzhi.lmSvtBack = "lm_huju_wenchen-lmback"
lm__buzhi.hidden = true
local lm_pingpan = fk.CreateTriggerSkill{
  name = "lm_pingpan",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and LM.getServantFromSkill(player, self.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local svt = LM.getServantFromSkill(player, self.name) --- 原则上不允许一个技能能对应多个将
    if svt and LM.removeServants(player, {svt.name}, player, nil, self.name) then
      player:drawCards(2, self.name)
      return true
    end
  end
}
lm__buzhi:addSkill(lm_pingpan)
Fk:loadTranslationTable{
  ["lm__buzhi"] = "步骘",
  ["lm_pingpan"] = "平叛",
  [":lm_pingpan"] = "锁定技，当你受到伤害时，移除此将领并防止之，然后摸两张牌。",
}

local lm__guyong = General(extension, "lm__guyong", "lm_k__ge", 0)
LM.setlordServant(lm_huju, "lm__guyong")
lm__guyong:addRelatedSkill("lm_huju_wenchen")
lm__guyong.lmSvtBack = "lm_huju_wenchen-lmback"
lm__guyong.hidden = true
local lm_shenxing = fk.CreateTriggerSkill{
  name = "lm_shenxing",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and not player:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    local slash = player.room:askForResponse(player, "slash", "slash", "#lm_shenxing-invoke")
    if slash then
      self.cost_data = slash
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:responseCard{
      from = player.id,
      card = self.cost_data
    }
    local armor = LM.GetCardByRule(room, ".|.|.|.|.|armor", 1, {Card.DrawPile, Card.DiscardPile})
    if #armor > 0 then
      room:obtainCard(player, armor, nil, fk.ReasonJustMove, player.id, self.name)
    end
  end
}
lm__guyong:addSkill(lm_shenxing)
Fk:loadTranslationTable{
  ["lm__guyong"] = "顾雍",
  ["lm_shenxing"] = "慎行",
  [":lm_shenxing"] = "回合结束时，你可以打出一张【杀】，然后从牌堆或弃牌堆内获得一张防具牌。",
  ["#lm_shenxing-invoke"] = "慎行：你可以打出一张【杀】，然后从牌堆或弃牌堆内获得一张防具牌",
}

local lm__zhugejin = General(extension, "lm__zhugejin", "lm_k__ge", 0)
LM.setlordServant(lm_huju, "lm__zhugejin")
lm__zhugejin:addRelatedSkill("lm_huju_wenchen")
lm__zhugejin.lmSvtBack = "lm_huju_wenchen-lmback"
lm__zhugejin.hidden = true
local lm_huanshi = fk.CreateTriggerSkill{
  name = "lm_huanshi",
  anim_type = "defensive",
  events = {fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryRound) == 0 and data.card and data.card.type == Card.TypeTrick
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#lm_huanshi-invoke:::" .. data.card:toLogString())
  end,
  on_use = function(self, event, target, player, data)
    table.insertIfNeed(data.nullifiedTargets, player.id)
  end
}
lm__zhugejin:addSkill(lm_huanshi)
Fk:loadTranslationTable{
  ["lm__zhugejin"] = "诸葛瑾",
  ["lm_huanshi"] = "缓释",
  [":lm_huanshi"] = "轮次技，当你成为锦囊牌的目标后，你可以令此牌对你无效。",
  ["#lm_huanshi-invoke"] = "缓释：你可以令 %arg 对你无效",
}

local lm__lusu = General(extension, "lm__lusu", "lm_k__ge", 0)
LM.setlordServant(lm_huju, "lm__lusu")
lm__lusu:addRelatedSkill("lm_huju_wenchen")
lm__lusu.lmSvtBack = "lm_huju_wenchen-lmback"
lm__lusu.hidden = true
local lm_dimeng = fk.CreateTriggerSkill{
  name = "lm_dimeng",
  anim_type = "drawcard",
  events = {LM.importedServants},
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      return #LM.getServantsFromSkill(self.name, data.svts) ~= 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askForSkillInvoke(player, self.name, nil, "#lm_dimeng-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:drawCards(player, 2, self.name)
    local targets = table.map(room:getOtherPlayers(player, false), Util.IdMapper)
    if #targets == 0 then return end
    local user = room:getPlayerById(room:askForChoosePlayers(player, targets, 1, 1, "#lm_dimeng-choose", self.name, false)[1])
    local _, dat = room:askForUseActiveSkill(user, "#lm_dimeng_active", "#lm_dimeng-use:" .. player.id, true, {lm_dimeng_victim = player.id})
    if dat then
      local card = Fk:cloneCard("snatch")
      card:addSubcards(dat.cards)
      card.skillName = self.name
      room:useCard{
        from = user.id,
        tos =  {{player.id}},
        card = card,
      }
    end
  end
}
local lm_dimeng_active = fk.CreateActiveSkill{
  name = "#lm_dimeng_active",
  anim_type = "offensive",
  card_num = 1,
  target_num = 0,
  card_filter = function(self, to_select, selected)
    if #selected == 0 then
      local card = Fk:cloneCard("snatch")
      card:addSubcard(to_select)
      card.skillName = "lm_dimeng"
      local to = Fk:currentRoom():getPlayerById(self.lm_dimeng_victim)
      return to and not to.dead and Self:canUseTo(card, to, {bypass_distances = true, bypass_times = true})
    end
  end,
}
Fk:addSkill(lm_dimeng_active)
lm__lusu:addSkill(lm_dimeng)
Fk:loadTranslationTable{
  ["lm__lusu"] = "鲁肃",
  ["lm_dimeng"] = "缔盟",
  [":lm_dimeng"] = "当你获得此将领后，你可以摸两张牌，然后指定一名其他角色，其可以将一张牌当【顺手牵羊】对你使用。",
  ["#lm_dimeng_active"] = "缔盟",
  ["#lm_dimeng-invoke"] = "缔盟：你可以摸两张牌，然后指定一名其他角色，其可以将一张牌当【顺手牵羊】对你使用",
  ["#lm_dimeng-choose"] = "缔盟：你指定一名其他角色，其可以将一张牌当【顺手牵羊】对你使用",
  ["#lm_dimeng-use"] = "缔盟：你可以将一张牌当【顺手牵羊】对 %src 使用",
}

local lm__lvmeng = General(extension, "lm__lvmeng", "lm_k__ge", 0)
LM.setlordServant(lm_huju, "lm__lvmeng")
lm__lvmeng:addRelatedSkill("lm_huju_wujiang")
lm__lvmeng.lmSvtBack = "lm_huju_wujiang-lmback"
lm__lvmeng.hidden = true
local lm_dujiang = fk.CreateTriggerSkill{
  name = "lm_dujiang",
  anim_type = "offensive",
  mute = true,
  events = {fk.TurnEnd, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      return player:hasSkill(self) and target and target ~= player
    else
      return target == player and data.to and not data.to.dead and
      data.card and data.card.trueName == "slash" and table.contains(data.card.skillNames, self.name)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnEnd then
      local use = room:askForUseCard(player, "slash", nil, "#lm_dujiang-ask::" .. target.id, true,
      { must_targets = {target.id}, exclusive_targets = {target.id}, bypass_distances = true, bypass_times = true })
      if use then
        self.cost_data = use
        return true
      end
    else
      local to = data.to
      if not to or #to:getCardIds("he") < 2 then return end
      return room:askForSkillInvoke(player, self.name, nil, "#lm_dujiang-invoke:" .. to.id)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnEnd then
      room:notifySkillInvoked(player, self.name)
      local use = self.cost_data
      room:useCard(use)
    else
      room:notifySkillInvoked(player, self.name, "control")
      local to = data.to
      local cards = room:askForCard(to, 2, 2, true, self.name, false, nil, "#lm_dujiang-give:" .. player.id)
      room:obtainCard(player, cards, false, fk.ReasonGive, to.id, self.name)
      return true
    end
  end,
}
lm__lvmeng:addSkill(lm_dujiang)
Fk:loadTranslationTable{
  ["lm__lvmeng"] = "吕蒙",
  ["lm_dujiang"] = "渡江",
  [":lm_dujiang"] = "其他角色回合结束时，你可以对其使用【杀】（无距离限制），此【杀】造成伤害时，你可以防止之并令受伤角色交给你两张牌。",
  ["#lm_dujiang-ask"] = "渡江：你可以对 %dest 使用【杀】（无距离限制）",
  ["#lm_dujiang-invoke"] = "渡江：你可以防止此【杀】对 %src 的伤害，令其交给你两张牌",
  ["#lm_dujiang-give"] = "渡江：你须交给 %src 两张牌。",
}

local lm__sunquan_zhouyu = General(extension, "lm__sunquan_zhouyu", "lm_k__ge", 0)
lm__sunquan_zhouyu.trueName = "zhouyu"
LM.setlordServant(lm_huju, "lm__sunquan_zhouyu")
lm__sunquan_zhouyu:addRelatedSkill(lm_huju_wujiang)
lm__sunquan_zhouyu.lmSvtBack = "lm_huju_wujiang-lmback"
lm__sunquan_zhouyu.hidden = true
local lm_yingzi = fk.CreateTriggerSkill{
  name = "lm_yingzi",
  anim_type = "drawcard",
  events = {LM.importedServants},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      if not table.find(data.svts, function(svt) return table.contains(LM.getServantSkills(svt), self.name) end) then return end
      local room = player.room
      local logic = room.logic
      local current_event = logic:getCurrentEvent()
      local end_id = 0
      local turn_event = current_event:findParent(GameEvent.Turn, true)
      if turn_event == nil then return false end
      room.logic:getEventsByRule(GameEvent.Turn, 1, function(e)
        if e.data[1] == player and e.id ~= turn_event.id then
          end_id = e.id
          return true
        end
        return false
      end, end_id)
      if end_id == 0 then return end
      local last = logic:getEventsByRule(GameEvent.UseCard, 1, function (e)
        local d = e.data[1] ---@type CardUseStruct
        local pid = d.from
        local c = d.card
        return pid == player.id and c and c.trueName == "slash"
      end, end_id)
      if last[1] then return true end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:drawCards(2, self.name)
  end,
}
lm__sunquan_zhouyu:addSkill(lm_yingzi)
Fk:loadTranslationTable{
  ["lm__sunquan_zhouyu"] = "周瑜",
  ["lm_yingzi"] = "英姿",
  [":lm_yingzi"] = "你获得此将领后，若你于你的上回合开始至今使用过【杀】，你摸两张牌。",
}

local lm__dingfeng = General(extension, "lm__dingfeng", "lm_k__ge", 0)
LM.setlordServant(lm_huju, "lm__dingfeng")
lm__dingfeng:addRelatedSkill("lm_huju_wujiang")
lm__dingfeng.lmSvtBack = "lm_huju_wujiang-lmback"
lm__dingfeng.hidden = true
local lm_duanbing = fk.CreateViewAsSkill{
  name = "lm_duanbing",
  prompt = "#lm_duanbing",
  anim_type = "offensive",
  pattern = "slash",
  card_num = 1,
  card_filter = function(self, to_select, selected)
    local card = Fk:getCardById(to_select)
    return #selected == 0 and card and card.number ~= 0 and card.number < 8
  end,
  view_as = function(self, cards)
    if #cards == 0 then return end
    local c = Fk:cloneCard("slash")
    c:addSubcards(cards)
    c.skillName = self.name
    return c
  end,
  before_use = function(self, player, use)
    local card = use.card
    if card and card.number == 1 then
      player:drawCards(2, self.name)
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name) < 2
  end,
  enabled_at_response = function(self, player, response)
    return player:usedSkillTimes(self.name) < 2
  end
}
lm__dingfeng:addSkill(lm_duanbing)
Fk:loadTranslationTable{
  ["lm__dingfeng"] = "丁奉",
  ["lm_duanbing"] = "短兵",
  [":lm_duanbing"] = "每回合限两次，你可以将点数小于7的牌当【杀】使用或打出，若为A则你摸两张牌。",
  ["#lm_duanbing"] = "你可以将点数小于7的牌当【杀】使用或打出，若为A则你摸两张牌。",
}

local lm__xusheng = General(extension, "lm__xusheng", "lm_k__ge", 0)
LM.setlordServant(lm_huju, "lm__xusheng")
lm__xusheng:addRelatedSkill("lm_huju_wujiang")
lm__xusheng.lmSvtBack = "lm_huju_wujiang-lmback"
lm__xusheng.hidden = true
local lm_pojun = fk.CreateTriggerSkill{
  name = "lm_pojun",
  anim_type = "offensive",
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and data.to and not data.to.dead and data.to:isKongcheng() and
    data.card and data.card.trueName == "slash" and data.by_user
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data.damage = data.damage + 1
  end,
}
local lm_pojun_targetmod = fk.CreateTargetModSkill{
  name = "#lm_pojun_targetmod",
  residue_func = function(self, player, skill, scope, card, to)
    if player:hasSkill("lm_pojun") and skill and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      return 1
    end
  end
}
lm_pojun:addRelatedSkill(lm_pojun_targetmod)
lm__xusheng:addSkill(lm_pojun)
Fk:loadTranslationTable{
  ["lm__xusheng"] = "徐盛",
  ["lm_pojun"] = "破军",
  [":lm_pojun"] = "你使用【杀】的次数上限+1；你对没有手牌的角色使用【杀】伤害+1。",
  ["#lm_pojun_targetmod"] = "破军",
}

local lm__sunquan_zhoutai = General(extension, "lm__sunquan_zhoutai", "lm_k__ge", 0)
lm__sunquan_zhoutai.trueName = "zhoutai"
LM.setlordServant(lm_huju, "lm__sunquan_zhoutai")
lm__sunquan_zhoutai:addRelatedSkill("lm_huju_wujiang")
lm__sunquan_zhoutai.lmSvtBack = "lm_huju_wujiang-lmback"
lm__sunquan_zhoutai.hidden = true
local lm_sizhan = fk.CreateTriggerSkill{
  name = "lm_sizhan",
  anim_type = "offensive",
  events = {fk.Damaged},
  on_cost =function(self, event, target, player, data)
    local room = player.room
    local use = U.askForUseVirtualCard(room, player, "slash", nil, self.name, "#lm_sizhan-use", true, true, false, true, nil, true)
    if use then
      self.cost_data = use
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:useCard(self.cost_data)
  end,
}
lm__sunquan_zhoutai:addSkill(lm_sizhan)
Fk:loadTranslationTable{
  ["lm__sunquan_zhoutai"] = "周泰",
  ["lm_sizhan"] = "死战",
  [":lm_sizhan"] = "当你受到伤害后，你可以视为使用一张【杀】。",
  ["#lm_sizhan-use"] = "死战：你可以视为使用一张【杀】",
}

local lm__ganning = General(extension, "lm__ganning", "lm_k__ge", 0)
LM.setlordServant(lm_huju, "lm__ganning")
lm__ganning:addRelatedSkill("lm_huju_wujiang")
lm__ganning.lmSvtBack = "lm_huju_wujiang-lmback"
lm__ganning.hidden = true
local lm_jieying = fk.CreateActiveSkill{
  name = "lm_jieying",
  prompt = "#lm_jieying",
  anim_type = "control",
  can_use = function(self, player)
    return player.phase == Player.Play and player:usedSkillTimes(self.name, Player.HistoryPhase) < 2
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 1,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id and not Fk:currentRoom():getPlayerById(to_select):isNude()
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:obtainCard(player, room:askForCardChosen(player, target, "he", self.name), false, fk.ReasonPrey, player.id, self.name)
    if target.dead then return end
    room:useVirtualCard("duel", nil, target, player, self.name)
  end
}
lm__ganning:addSkill(lm_jieying)
Fk:loadTranslationTable{
  ["lm__ganning"] = "甘宁",
  ["lm_jieying"] = "劫营",
  [":lm_jieying"] = "出牌阶段限两次，你可以获得一名其他角色一张牌，然后其视为对你使用一张【决斗】。",
  ["#lm_jieying"] = "你可以获得一名其他角色一张牌，然后其视为对你使用一张【决斗】",
}

local lm__luxun = General(extension, "lm__luxun", "lm_k__ge", 0)
lm__luxun.lmSvtBack = "lm_huju-lmback"
lm__luxun.hidden = true
local lm_cuike = fk.CreateActiveSkill{
  name = "lm_cuike",
  prompt = "#lm_cuike",
  anim_type = "control",
  can_use = function(self, player)
    return player.phase == Player.Play and #LM.getServantsFromSkill(self.name, LM.getServants(player)) > 0
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 0,
  target_filter = Util.FalseFunc,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local svtname = "lm__luxun"
    if not LM.getServant(player, svtname) then return end
    LM.removeServants(player, {svtname}, player, true, self.name)
    local targets = table.filter(room:getOtherPlayers(player, false), function(lord)
      return #LM.getServants(lord) ~= 0
    end)
    if #targets == 0 then return end
    local target = room:getPlayerById(room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#lm_cuike-ask", self.name, false)[1])
    LM.removeServantsByChc(target, 1, nil, player, true, self.name)
    if target.dead then return end
    room:damage{
      from = player,
      to = target,
      damage = 1,
      damageType = fk.FireDamage,
      skillName = self.name
    }
  end
}
lm__luxun:addSkill(lm_cuike)
Fk:loadTranslationTable{
  ["lm__luxun"] = "陆逊",
  ["lm_cuike"] = "摧克",
  [":lm_cuike"] = "出牌阶段，你可以将此将领移出游戏，然后将一名其他角色的一名将领移出游戏，再对其造成1点火焰伤害。",
  ["#lm_cuike"] = "你可以将陆逊移出游戏，然后将一名其他角色的一名将领移出游戏，再对其造成1点火焰伤害",
  ["#lm_cuike-ask"] = "摧克：你须将一名其他角色的一名将领移出游戏，再对其造成1点火焰伤害。",
}

return extension